/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.mindlistening.tray;

import com.googlecode.mindlistening.events.ExitApplicationMessage;
import com.googlecode.mindlistening.events.TrayMessage;
import java.awt.AWTException;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JWindow;
import org.bushe.swing.event.EventBus;
import org.bushe.swing.event.EventSubscriber;

/**
 * This class represents the tray.
 *
 * @author ryan
 */
public class Tray   {


    public static final String MENU_SEPERATOR = "SEPERATOR";

    private TrayIcon trayIcon;
    private Messages messages;
    private Image baseImage;


    protected Tray() {
        // for unit tests
    }

    public Tray(String iconLocation, Messages messages, List popupItems) {
        this.messages = messages;
        this.baseImage = createImage(iconLocation);
        SystemTray tray = SystemTray.getSystemTray();
        if (!SystemTray.isSupported()) {
            throw new RuntimeException(messages.getLocalMessage(Messages.TRAY_NOT_SUPPORTED));
        }
        trayIcon = new TrayIcon(createImage(iconLocation), messages.getLocalMessage(Messages.APP_NAME));
        PopupMenu popup = createMenu(popupItems);

        trayIcon.setPopupMenu(popup);

        try {
            tray.add(trayIcon);
            registerEvents();
        } catch (AWTException e) {
            throw new RuntimeException(messages.getLocalMessage(Messages.TRAY_NOT_SUPPORTED));
        }

    }


   protected void registerEvents() {

       EventBus.subscribeStrongly(TrayMessage.class, new EventSubscriber<TrayMessage>() {
            @Override
            public void onEvent(TrayMessage t) {
                System.out.println("Tray Message: " + t.getMessage());
                trayIcon.displayMessage(t.getTitle(), t.getMessage(), t.getType());
            }
        });

        EventBus.subscribeStrongly(ExitApplicationMessage.class, new EventSubscriber<ExitApplicationMessage>() {

            @Override
            public void onEvent(ExitApplicationMessage t) {
                SystemTray tray = SystemTray.getSystemTray();
                tray.remove(trayIcon);

                

            }
        });
   }



    protected final PopupMenu createMenu(List popupItems) {
        final PopupMenu popup = new PopupMenu("EckoIT Menu");
        for (Object object : popupItems) {
            if (object instanceof MenuItem) {
                popup.add((MenuItem)object);
            }
            if (MENU_SEPERATOR.equals(object)) {
                popup.addSeparator();
            }
        }
        return popup;
    }




    //Obtain the image URL
    protected static Image createImage(String path) {
        URL imageURL = Tray.class.getResource(path);

        if (imageURL == null) {
            System.err.println("Resource not found: " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL)).getImage();
        }

    }



}
